package com.ly.hotel.adaptor;


import java.sql.Date;
import java.text.SimpleDateFormat;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;
import android.view.KeyEvent;

import com.ly.hotel.board.ADA628.BoardInterfaceAda628;
import com.ly.hotel.board.ADA648.BoardInterfaceAda648;
import com.ly.hotel.common.Constants;
import com.ly.hotel.common.HandNoBinder;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.SharedPreferencesConfig;
import com.ly.hotel.util.TouchUtil;

/**
 * @author Jouson 与电视接口有关的工具
 */
public class HardwareInterface {
	private static AudioManager audioManager=null;
	private static int maxVloume = 10;
	/*初始化电视*/
	private static void initSerialBaud(Context c) {
		MyApplication app = (MyApplication) c.getApplicationContext();
		
		app.setSerialBaud(SharedPreferencesConfig.getSerialBaud(c));
	}

	public static void init(Context c){
		Log.e("borad", Build.BOARD + "," + Build.VERSION.SDK_INT + "," + Build.VERSION_CODES.HONEYCOMB);
		initSerialBaud(c);				// 从配置获取串口波特率
        audioManager = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
		
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			BoardInterfaceAda628.init(c);
			setCardLed(c, false);
//			BoardInterfaceAda628.hardwareInit();
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_RK3288_NAME)){
			BoardInterfaceAda648.hardwareInit();
		}
	}

	private static boolean inited = false;
	public static void initSerial(Context c , boolean reInit){
		if(!inited || reInit){
			inited = true;
			//只有两个都为空的情况下才初始化
			if(HandNoBinder.sp == null && HandNoBinder.sp2 == null){
				initSerialBaud(c);				// 从配置获取串口波特率
				
				if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
					BoardInterfaceAda648.hardwareInit();
				}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
					BoardInterfaceAda628.hardwareInit();
				}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_A83T_NAME)){
					BoardInterfaceAda628.hardwareInit();
				}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_S905_WY_NAME)){
					BoardInterfaceAda628.hardwareInit();
				}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_6A648_NAME) ||
						Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA648_NAME)){
					BoardInterfaceAda648.hardwareInit();
				}
			}
		}
	}
	
	//静音=true
	//取消静音=false
	public static void setMuteFlag(boolean state){
	    //采用android 4.4的静音
	    if(audioManager != null){
		    if (state) {
                audioManager.setStreamMute(AudioManager.STREAM_MUSIC , true);
            } else {
                audioManager.setStreamMute(AudioManager.STREAM_MUSIC , false);
            }
	    }
	}
	
	public static void setVolume(int volume){
	    if(audioManager != null){
		    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
	    }
	}
	
	public static int getVolume(){
		int volume = 0;
	    if(audioManager != null){
		    volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
	    }
		return volume;
	}

	//发送广播，服务灯、按键灯
	public static void setGpioDeviceStatus(boolean enable,int type){
		Intent intent = new Intent("com.ly.hotel.adaptor.MSTAR");
		if(enable){
			intent.putExtra("type",type);  //亮灯广播
		}else{
			intent.putExtra("type",type);  //灭灯广播
		}
		intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
		MyApplication.getContext().sendBroadcast(intent);
	}
	
	public static void setPowerLedOff(){

		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
//			BoardInterfaceAda801.setPowerLedOff();
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			
		}
	}

	public static void setPowerLedGreen(){

		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
//			BoardInterfaceAda801.setPowerLedGreen();
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			
		}
	}
	
	public static void setPowerLedRed(){

		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
//			BoardInterfaceAda801.setPowerLedRed();
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			
		}
	}
	
	//获取显示屏的亮度
	public int getScreenBrighten(Context context){
		
		int brighten = 0;
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
//			brighten = BoardInterfaceAda801.getScreenBrighten(context);
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			Log.e("adaptor-tvinterface", "不支持getScreenBrighten");
		}
		
		return brighten;
	}
	//设置显示屏亮度
	public void setScreenBrighten(Context context, int value){
		
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
//			BoardInterfaceAda801.setScreenBrighten(context,value);
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			Log.e("adaptor-tvinterface", "不支持setScreenBrighten");
		}

	}
	
	//获取显示屏背光
	public static int getLcdBrighten(Context context){
		int brighten = 0;
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
//			brighten = BoardInterfaceAda801.getLcdBrighten(context);
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
//			Log.e("adaptor-tvinterface", "不支持getLcdBrighten");
			brighten = BoardInterfaceAda628.getLcdBrighten(context);
		}
		
		return brighten;
	}
	//设置显示屏背光
	public static void setLcdBrighten(Context context, int value){
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
//			BoardInterfaceAda801.setLcdBrighten(context,value);
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
//			Log.e("adaptor-tvinterface", "不支持setLcdBrighten");
			BoardInterfaceAda628.setLcdBrighten(context,value);
		}

	}

	//设置显示屏背光
	public static void setCardLed(Context context, boolean on){
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			BoardInterfaceAda628.setCardLed(on);
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
			Log.e("adaptor-tvinterface", "不支持setCardLed");
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_6A648_NAME) ||
				Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA648_NAME)){
			BoardInterfaceAda628.setCardLed(on);
		}

	}
	
	public static String getBoardNameAndVersion(){
		String s = null;

		int w = MyApplication.screenWidth;
		long time = Build.TIME;
		Date date = new Date(time);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
			s = "PRE" + "_" + w + "_" + format.format(date);
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			s = "PRE" + "_" + w + "_" + format.format(date);
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_6A648_NAME)){
			s = "PRE4" + "_" + w + "_" + format.format(date);
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA648_NAME)){
			s = "PRE4" + "_" + w + "_" + format.format(date) + "A";
		}else{
			s = Build.DISPLAY;
		}

		return s;
	}
	//恢复视窗为全屏模式
	public static void setFullScreen(){

		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
//			BoardInterfaceAda801.setFullScreen();
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			BoardInterfaceAda628.setFullScreen();
		}
		
	}
	
	public static void rebootSystem() {

		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
//			BoardInterfaceAda801.rebootSystem();
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			BoardInterfaceAda628.rebootSystem();
		}
		
	}
	
	public static void standbySystem() {
		//待机要把指示灯状态变回红色
/*		setPowerLedRed();
		
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
			BoardInterfaceAda801.standbySystem();
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			BoardInterfaceAda628.standbySystem();
		}
		*/ 
		TouchUtil.sendKeyCode(KeyEvent.KEYCODE_POWER, 0, true);
	}
	
	//获取最大音量
	public static int getMaxVolume() {
		return maxVloume;
	}
	
	//这个是设置到mstar方案主板的音量最大值，其实就是为了防止遥控器或者电视的音量按键控制音量太大声
	//当然啦，普通安卓主板不需要这个鬼东西了！！！！！！！！
	public static void setMaxVolume(int value) {
		maxVloume = value;
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
//			BoardInterfaceAda801.setMaxVolume(value);
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			BoardInterfaceAda628.setMaxVolume(value);
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_6A648_NAME) ||
				Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA648_NAME)){
			BoardInterfaceAda648.setMaxVolume(value);
		}
	}

	public static void setAtvChannel(Integer position) {
		// TODO Auto-generated method stub
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
			Log.e("adaptor-tvinterface", "不支持setAtvChannel");
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			BoardInterfaceAda628.setAtvChannel(position);
		}
	}

	public static void writeBytesToI2C(int i, short[] address, short[] buffer) {
		// TODO Auto-generated method stub
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
			Log.e("adaptor-tvinterface", "不支持writeBytesToI2C");
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			BoardInterfaceAda628.writeBytesToI2C(i, address, buffer);
		}
	}

	public static void setInputSourceStorage() {
		// TODO Auto-generated method stub
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
			Log.e("adaptor-tvinterface", "不支持setInputSourceStorage");
		}else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			BoardInterfaceAda628.setInputSourceStorage();
		}
	}

	public static void setCallGpioStatus(boolean enable){
		// TODO Auto-generated method stub
		if(Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA801_NAME)){
			BoardInterfaceAda628.setCallGpioStatus(enable);
		} else if (Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA628_NAME)){
			BoardInterfaceAda628.setCallGpioStatus(enable);
		} else if(Build.BOARD.endsWith(Constants.BOARD_TYPE_6A648_NAME) ||
				Build.BOARD.endsWith(Constants.BOARD_TYPE_ADA648_NAME)){
			BoardInterfaceAda648.setCallGpioStatus(enable);
		}
	}

}
